package com.erp.serviceImpl.baseDataServiceImpl;

import com.erp.dao.AccountMapper;
import com.erp.pojo.Account;
import com.erp.pojo.AccountExample;
import com.erp.service.baseDataService.AccountService;
import com.erp.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Author:${JayC}
 * Date:2019/1/19
 * Time:9:59
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    Msg msg = new Msg();
    @Override
    public Msg getAcc(String name,String serialno, int page, int limit) {
        AccountExample example = new AccountExample();

        AccountExample.Criteria criteria= example.createCriteria();

        if (name!=null && !"".equals(name)){
            name="%"+name+"%";
            criteria.andNameLike(name);
        }
        if (serialno!=null&&!"".equals(serialno)){
            serialno="%"+serialno+"%";
            criteria.andSerialnoLike(serialno);
        }

        PageHelper.startPage(page,limit);

        List<Account> list = accountMapper.selectByExample(example);

        PageInfo<Account> pagelist = new PageInfo<>(list);

        msg.setCode(0);
        msg.setCount(pagelist.getTotal());
        msg.setMsg("");
        msg.setData(pagelist.getList());

        System.out.println(msg);

        return msg;
    }

    @Override
    public void deleteAcc(Long id) {

        accountMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insertAcc(Account account) {
        accountMapper.insertSelective(account);
    }
}
